package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.help.menu.InitMenuHelp;
import com.xbongbong.paas.pojo.dto.FormListInProcessDTO;
import com.xbongbong.paas.pojo.dto.MenuCopyDTO;
import com.xbongbong.paas.pojo.dto.MenuFormAddDTO;
import com.xbongbong.paas.pojo.dto.MenuFormDelDTO;
import com.xbongbong.paas.pojo.dto.MenuGroupAddDTO;
import com.xbongbong.paas.pojo.dto.MenuGroupDelDTO;
import com.xbongbong.paas.pojo.dto.MenuListDTO;
import com.xbongbong.paas.pojo.dto.MenuNameCheckDTO;
import com.xbongbong.paas.pojo.dto.MenuReportAddDTO;
import com.xbongbong.paas.pojo.dto.MenuReportDelDTO;
import com.xbongbong.paas.pojo.dto.MenuSortDTO;
import com.xbongbong.paas.pojo.dto.MenuUpdateDTO;
import com.xbongbong.paas.pojo.dto.ProcessFormListDTO;
import com.xbongbong.paas.pojo.vo.FormListInProcessVO;
import com.xbongbong.paas.pojo.vo.MenuCopyVO;
import com.xbongbong.paas.pojo.vo.MenuFormAddVO;
import com.xbongbong.paas.pojo.vo.MenuFormDelVO;
import com.xbongbong.paas.pojo.vo.MenuGroupAddVO;
import com.xbongbong.paas.pojo.vo.MenuGroupDelVO;
import com.xbongbong.paas.pojo.vo.MenuListVO;
import com.xbongbong.paas.pojo.vo.MenuNameCheckVO;
import com.xbongbong.paas.pojo.vo.MenuReportAddVO;
import com.xbongbong.paas.pojo.vo.MenuReportDelVO;
import com.xbongbong.paas.pojo.vo.MenuSortVO;
import com.xbongbong.paas.pojo.vo.MenuUpdateVO;
import com.xbongbong.paas.pojo.vo.ProcessFormListVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.service.PaasMenuService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.helper.ApiHttpRequestUtils;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.menu.pojo.dto.ControlCenterMenuListDTO;
import com.xbongbong.pro.menu.pojo.dto.ControlCenterMenuSetListDTO;
import com.xbongbong.pro.menu.pojo.dto.ControlCenterMenuSortDTO;
import com.xbongbong.pro.menu.pojo.dto.ControlMenuSetDTO;
import com.xbongbong.pro.menu.pojo.dto.HomeMenuSetListDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuChangeStateDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuNewListDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuQueryDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuSetDTO;
import com.xbongbong.pro.menu.pojo.dto.MenuTopDTO;
import com.xbongbong.pro.menu.pojo.vo.ControlCenterMenuListVO;
import com.xbongbong.pro.menu.pojo.vo.ControlCenterMenuSetListVO;
import com.xbongbong.pro.menu.pojo.vo.ControlCenterMenuSortVO;
import com.xbongbong.pro.menu.pojo.vo.ControlMenuSetVO;
import com.xbongbong.pro.menu.pojo.vo.HomeMenuSetListVO;
import com.xbongbong.pro.menu.pojo.vo.MenuChangeStateVO;
import com.xbongbong.pro.menu.pojo.vo.MenuNewListVO;
import com.xbongbong.pro.menu.pojo.vo.MenuQueryVO;
import com.xbongbong.pro.menu.pojo.vo.MenuSetVO;
import com.xbongbong.pro.menu.pojo.vo.MenuTopVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Pro项目菜单管理相关接口，有相同前缀的还有PaasMenuController，注意不要重复
 * @author kaka
 * @time 2019/1/3 21:02
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/menu")
public class ProMenuController {

    private static final Logger LOG = LoggerFactory.getLogger(ProMenuController.class);

    @Resource
    private PaasMenuService paasMenuService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private InitMenuHelp initMenuHelp;

    /**
     * 添加报表菜单
     * @param menuReportAddDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addReport", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addReport(@RequestBody @Valid MenuReportAddDTO menuReportAddDTO, BindingResult br) throws Exception {
        XbbResponse<MenuReportAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {

            try {
                MenuReportAddVO menuReportAddVO = paasMenuService.addReportMenu(menuReportAddDTO);
                response = new XbbResponse<>(menuReportAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     * 删除报表，需要判断表单是否有数据
     * @param menuReportDelDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deleteReportForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteReportForm(@RequestBody @Valid MenuReportDelDTO menuReportDelDTO, BindingResult br) throws Exception {
        XbbResponse<MenuReportDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuReportDelVO menuReportDelVO = paasMenuService.deleteReportMenu(menuReportDelDTO);
                response = new XbbResponse<>(menuReportDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 菜单列表
     * @param menuListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid MenuListDTO menuListDTO, BindingResult br) throws Exception {
        XbbResponse<MenuListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuListVO menuListVO = paasMenuService.listMenu(menuListDTO);
                response = new XbbResponse<>(menuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 菜单排序
     * @param menuSortDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateSort(@RequestBody @Valid MenuSortDTO menuSortDTO, BindingResult br) throws Exception {
        XbbResponse<MenuSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuSortVO menuSortVO = paasMenuService.sortMenu(menuSortDTO);
                response = new XbbResponse<>(menuSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加分组
     * @param menuGroupAddDTO 前端传入对象
     * @param br 对象校验结果
     * @return 返回结果集
     * @throws Exception
     */
    @RequestMapping(value = "/addGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addGroup(@RequestBody @Valid MenuGroupAddDTO menuGroupAddDTO, BindingResult br) throws Exception {
        XbbResponse<MenuGroupAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuGroupAddDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuGroupAddDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                MenuGroupAddVO menuGroupAddVO = paasMenuService.addMenu(menuGroupAddDTO);
                response = new XbbResponse<>(menuGroupAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加表单菜单
     * @param menuFormAddDTO 前端传入对象
     * @param br 对象校验结果
     * @return 返回结果集
     * @throws Exception
     */
    @RequestMapping(value = "/addForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addForm(@RequestBody @Valid MenuFormAddDTO menuFormAddDTO, BindingResult br) throws Exception {
        XbbResponse<MenuFormAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuFormAddDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuFormAddDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                MenuFormAddVO menuFormAddVO = paasMenuService.addFormMenu(menuFormAddDTO);
                response = new XbbResponse<>(menuFormAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 添加表单菜单
     * @param menuNameCheckDTO 前端传入对象
     * @param br 对象校验结果
     * @return 返回结果集
     * @throws Exception
     */
    @RequestMapping(value = "/checkName", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkName(@RequestBody @Valid MenuNameCheckDTO menuNameCheckDTO, BindingResult br) throws Exception {
        XbbResponse<MenuNameCheckVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuNameCheckDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuNameCheckDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                MenuNameCheckVO menuNameCheckVO = paasMenuService.checkTemplateName(menuNameCheckDTO);
                response = new XbbResponse<>(menuNameCheckVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 应用&菜单 开启/关闭接口
     * 可以同时支持应用和菜单的开启关闭操作，
     *
     */
    @RequestMapping(value = "/changeState", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String changeState(@RequestBody @Valid MenuChangeStateDTO menuChangeStateDTO, BindingResult br) throws Exception {
        XbbResponse<MenuChangeStateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuChangeStateDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuChangeStateDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {
                MenuChangeStateVO menuChangeStateVO = paasMenuService.changeState(menuChangeStateDTO);
                response = new XbbResponse<>(menuChangeStateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     * 关闭第三方应用
     *
     */
    @RequestMapping(value = "/closeOpenApp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String closeOpenApp(@RequestBody @Valid MenuChangeStateDTO menuChangeStateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(menuChangeStateDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(menuChangeStateDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            try {

                JSONObject header = new JSONObject();
                header.put("secret", PaasConstant.API_SECRET);
                header.put("openAppId", "1234");

                String url = ProBaseConfig.getApiUrl() + "/pro/v1/app/cancelAuthorizeInXbb";
                JSONObject data = new JSONObject();
                data.put("corpid", menuChangeStateDTO.getCorpid());
                data.put("userId", menuChangeStateDTO.getUserId());
                String ret = ApiHttpRequestUtils.post(header, url, data.toJSONString());

                LOG.info("ret:" + ret);

                JSONObject retJson = JSON.parseObject(ret);
                Integer code = retJson.getInteger("code");
                String msg = retJson.getString("msg");
                if (!Objects.equals(retJson.getInteger("code"), ErrorCodeEnum.API_SUCCESS.getCode())) {
                    throw new XbbException(code, msg);
                }
                response = new XbbResponse<>(new BaseVO());
            } catch (XbbException e) {
                e.printStackTrace();
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 更新菜单数据
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String update(@RequestBody @Valid MenuUpdateDTO menuUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<MenuUpdateVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuUpdateVO menuUpdateVO = paasMenuService.updateMenu(menuUpdateDTO);
                response = new XbbResponse<>(menuUpdateVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除分组，需要判断分组下是否有表单或者报表
     */
    @RequestMapping(value = "/deleteGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteGroup(@RequestBody @Valid MenuGroupDelDTO menuGroupDelDTO, BindingResult br) throws Exception {
        XbbResponse<MenuGroupDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuGroupDelVO menuGroupDelVO = paasMenuService.deleteMenu(menuGroupDelDTO);
                response = new XbbResponse<>(menuGroupDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 删除表单，需要判断表单是否有数据
     */
    @RequestMapping(value = "/deleteForm", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteForm(@RequestBody @Valid MenuFormDelDTO menuFormDelDTO, BindingResult br) throws Exception {
        XbbResponse<MenuFormDelVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuFormDelVO menuFormDelVO = paasMenuService.deleteFormMenu(menuFormDelDTO);
                response = new XbbResponse<>(menuFormDelVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 复制菜单
     */
    @RequestMapping(value = "/copy", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String copy(@RequestBody @Valid MenuCopyDTO menuCopyDTO, BindingResult br) throws Exception {
        XbbResponse<MenuCopyVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuCopyVO menuCopyVO = paasMenuService.copy(menuCopyDTO);
                response = new XbbResponse<>(menuCopyVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 查询应用中的流程列表，即用于发起流程显示
     */
    @RequestMapping(value = "/process/form/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String processFormList(@RequestBody @Valid ProcessFormListDTO processFormListDTO, BindingResult br) throws Exception {
        XbbResponse<ProcessFormListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ProcessFormListVO processFormListVO = paasMenuService.getProcessFormList(processFormListDTO);
                response = new XbbResponse<>(processFormListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     * 流程菜单
     * @param formListInProcessDTO
     */
    @RequestMapping(value = "/list/in/process", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String formListInProcess(@RequestBody @Valid FormListInProcessDTO formListInProcessDTO, BindingResult br) throws Exception {
        XbbResponse<FormListInProcessVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                FormListInProcessVO formListInProcessVO = paasMenuService.formListInProcess(formListInProcessDTO);
                response = new XbbResponse<>(formListInProcessVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 带应用的菜单列表
     */
    @RequestMapping(value = "/home/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid MenuNewListDTO menuListDTO, BindingResult br) throws Exception {
        XbbResponse<MenuNewListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuNewListVO menuListVO = paasMenuService.list(menuListDTO);
                response = new XbbResponse<>(menuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 顶部菜单信息
     */
    @RequestMapping(value = "/top/info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String topMenuList(@RequestBody @Valid MenuTopDTO menuTopDTO, BindingResult br) throws Exception {
        XbbResponse<MenuTopVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuTopVO menuTopVO = paasMenuService.topMenuList(menuTopDTO);
                response = new XbbResponse<>(menuTopVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 个人快捷新建和常用表单设置
     */
    @RequestMapping(value = "/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String set(@RequestBody @Valid MenuSetDTO menuSetDTO, BindingResult br) throws Exception {
        XbbResponse<MenuSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuSetVO menuSetVO = paasMenuService.set(menuSetDTO);
                response = new XbbResponse<>(menuSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 公司快捷新建和常用表单设置
     */
    @RequestMapping(value = "/control/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String controlSet(@RequestBody @Valid ControlMenuSetDTO controlMenuSetDTO, BindingResult br) throws Exception {
        XbbResponse<ControlMenuSetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ControlMenuSetVO menuSetVO = paasMenuService.controlSet(controlMenuSetDTO);
                response = new XbbResponse<>(menuSetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 管理中心菜单栏
     * @param controlCenterMenuListDTO 前端传入对象
     * @param br 对象校验结果
     * @return 返回管理中心菜单栏
     * @throws Exception
     */
    @RequestMapping(value = "/controlCenter/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String controlCenterMenuList(@RequestBody @Valid ControlCenterMenuListDTO controlCenterMenuListDTO, BindingResult br) throws Exception{
        XbbResponse<ControlCenterMenuListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ControlCenterMenuListVO controlCenterMenuListVO = paasMenuService.controlCenterMenuList(controlCenterMenuListDTO);
                response = new XbbResponse<>(controlCenterMenuListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 管理中心-菜单设置-菜单列表
     */
    @RequestMapping(value = "/controlCenter/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String controlCenterMenuSetList(@RequestBody @Valid ControlCenterMenuSetListDTO controlCenterMenuSetListDTO, BindingResult br) throws Exception{
        XbbResponse<ControlCenterMenuSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ControlCenterMenuSetListVO controlCenterMenuSetList = paasMenuService.controlCenterMenuSetList(controlCenterMenuSetListDTO);
                response = new XbbResponse<>(controlCenterMenuSetList);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 首页-菜单设置-菜单列表
     * @param homeMenuSetListDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/home/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String homeMenuSetList(@RequestBody @Valid HomeMenuSetListDTO homeMenuSetListDTO, BindingResult br) throws Exception{
        XbbResponse<HomeMenuSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                HomeMenuSetListVO homeMenuSetListVO = paasMenuService.homeMenuSetList(homeMenuSetListDTO);
                response = new XbbResponse<>(homeMenuSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 管理中心-菜单设置-菜单排序
     */
    @RequestMapping(value = "/controlCenter/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String controlCenterMenuSort(@RequestBody @Valid ControlCenterMenuSortDTO controlCenterMenuSortDTO, BindingResult br) throws Exception{
        XbbResponse<ControlCenterMenuSortVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ControlCenterMenuSortVO controlCenterMenuSortVO = paasMenuService.controlCenterMenuSort(controlCenterMenuSortDTO);
                response = new XbbResponse<>(controlCenterMenuSortVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 管理中心-菜单设置-菜单搜索
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String query(@RequestBody @Valid MenuQueryDTO menuQueryDTO, BindingResult br) throws Exception{
        XbbResponse<MenuQueryVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MenuQueryVO menuQueryVO = paasMenuService.queryMenuByName(menuQueryDTO);
                response = new XbbResponse<>(menuQueryVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
